import { ref, watch } from "vue"
import type { TodoListType } from "./model/todoModel"

export default function (key: string, value?: TodoListType) {
  let data = ref(value)

  if (value) {
    window.localStorage.setItem(key, JSON.stringify(value))
  } else {
    data.value = JSON.parse(window.localStorage.getItem(key)!)
  }

  watch(data, (newValue) => {
    window.localStorage.setItem(key, JSON.stringify(newValue))
  })

  return data;
}